package com.ruibei.ui;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.ruibei.R;
import com.ruibei.ui.fragment.ApprovalGetFragment;
import com.ruibei.ui.fragment.ApprovalSendFragment;
import com.ruibei.view.PagerSlidingTabStrip;
import com.ruibei.view.TitleBarView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * 审批页面（包含申请的、审批的）
 * 
 * @author limingyan
 * 
 */
public class ApprovalActivity extends BaseActivity {

	@InjectView(R.id.work_approval_title_bar)
	TitleBarView mTitleBar;
	// tab选项卡控制类
	@InjectView(R.id.work_approval_tabs)
	PagerSlidingTabStrip mTabs;
	@InjectView(R.id.work_approval_pager)
	ViewPager mPager;

	private MyPagerAdapter mAdapter;
	String[] titles = { "申请的", "审批的" };
	private ApprovalSendFragment approvalSendFragment;// 申请的页面
	private ApprovalGetFragment approvalGetFragment;// 收到审批的页面

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_approval);
		ButterKnife.inject(this);

		initView();
	}

	private void initView() {
		mTitleBar.setTitleText("工作审批");
		mTitleBar.setBtnLeft(R.drawable.back, R.string.back);
		mTitleBar.setBtnLeftOnclickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		mAdapter = new MyPagerAdapter(getSupportFragmentManager(),titles);
		mPager.setAdapter(mAdapter);
		
		final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
				.getDisplayMetrics());
		mPager.setPageMargin(pageMargin);
		mTabs.setViewPager(mPager);
		
	}

	public class MyPagerAdapter extends FragmentPagerAdapter {

		String[] _titles;

		public MyPagerAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}

		@Override
		public int getCount() {
			return _titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				//申请的页面
				if (approvalSendFragment == null) {
					approvalSendFragment = new ApprovalSendFragment();
				}
				return approvalSendFragment;
			case 1:
				//审批的页面
				if (approvalGetFragment == null) {
					approvalGetFragment = new ApprovalGetFragment();
				}
				return approvalGetFragment;
			default:
				return null;
			}
		}
	}
}
